package com.project.im.web.config;

import com.alibaba.fastjson.JSON;
import com.project.im.app.vo.ResponseVO;
import com.project.im.common.enums.ResponseCode;
import com.project.im.common.util.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import java.io.IOException;

@Slf4j
public class WebAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.info("【鉴权失败】请求地址: {} 异常信息: {}", request.getRequestURI(), accessDeniedException.getMessage());
        WebUtils.write(response, JSON.toJSONString(ResponseVO.fail(ResponseCode.PR100)));
    }
}
